import store from '../store/index.js'
// 登录中
function userLogin(share_id,fromJin,redirect_url=location.href,arg={}){
	return  new Promise((resolve, reject) => {
		console.log(store.getters.getToken,!store.getters.getToken,'!store.getters.getToken');
			if(!store.getters.getToken){
				arg.redirect_url=encodeURI(redirect_url)
				if(share_id){
					arg.share_id=share_id
				}
				if(fromJin){
					arg.fromJin=fromJin
				}
				let str_arg=convertObj(arg)
				console.log(store.state.URL+"login/Login/login?"+str_arg,'TTTTTTTTTTTTT');
				window.location.href=store.state.URL+"login/Login/login?"+str_arg	
				console.log(window.location.href,'SAAAAAAAAAAAAAAAAAAAAA');
			}
			resolve()
		})
}

function convertObj(data) {
    var _result = [];
    for (var key in data) {
      var value = data[key];
      if (value.constructor == Array) {
        value.forEach(function(_value) {
          _result.push(key + "=" + _value);
        });
      } else {
        _result.push(key + '=' + value);
      }
    }
    return _result.join('&');
}
// 登录中
// async function login(){
// 	await _login()
// }
function  login(){
	return  new Promise((resolve, reject) => {
		let self = this
		// let token=store.getters.getToken
		let token=store.state.token
		uni.request({
			url:store.state.URL + 'user/api_index/info',
			data:{
				token: token,
			},
			method:"POST",
			success(res) {
				if(res.data.status) {
					store.commit('updataUserId',res.data.msg.id)
					store.commit('updataUserInfo',res.data.msg)
					uni.setStorageSync("userInfo",res.data.msg)
					// 将用户的id保存在本地存储
					uni.setStorageSync("userId",res.data.msg.id)
				}
				resolve(res.data)
			}
		})
	})
	
}

// 检查是否已授权
function testGet(){
	if(!res.authSetting['scope.userInfo']){
		newInfo()
		uni.showToast({
			title:"请先登录",
			icon:"none",
			duration:2000
		})
	}else{
		console.log('11111111111111')
	}
}
module.exports = {
	userLogin,
	testGet,
	// login
}